package routes

import (
	"KD_web/go/controller"
	"KD_web/go/myUtils"
	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
	"time"
)

func SetRouter() *gin.Engine {
	r := gin.Default()

	// 配置 CORS
	r.Use(cors.New(cors.Config{
		AllowOrigins:     []string{"http://localhost:5173"},
		AllowMethods:     []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
		AllowHeaders:     []string{"Origin", "Content-Type", "Authorization"},
		ExposeHeaders:    []string{"Content-Length"},
		AllowCredentials: true,
	}))

	// 设置静态文件目录
	r.Static("/img", "./resources/avatars")
	r.POST("/upload", controller.UploadAvatar)

	/*
		用户路由组
	*/
	userGroup := r.Group("user")
	{
		userGroup.POST("/create", myUtils.RateLimiter(1, 24*time.Hour), controller.CreateUser)
		userGroup.GET("/users", controller.GetUserList)
		userGroup.GET("/users/:id", controller.GetUserById)
		userGroup.PUT("/users/:id", controller.UpdateUser)
		userGroup.DELETE("/users/:id", controller.DeleteUser)
		userGroup.PUT("role/:id", controller.UpdateUserRole)
		userGroup.POST("/login", controller.Login)
	}
	/*
		表格路由组
	*/
	tableGroup := r.Group("table")
	{
		tableGroup.GET("tables/:id", controller.GetTablesByUserId)
		tableGroup.POST("create", controller.CreateTableWithUserId)
		tableGroup.DELETE("tables/:id", controller.DeleteTableByTableId)
	}
	/*
		结论路由组
	*/
	commentGroup := r.Group("comment")
	{
		commentGroup.GET("comments/:id", controller.GetCommentByTableId)
		commentGroup.POST("create", myUtils.RateLimiter(30, time.Minute), controller.CreateComment)
	}

	return r
}
